var KOC = require('koc.common.utils');

var StateData = require("../statedata");
var DALFactory = require("../../lib/dal/dal-factory");
var CommonUtils = require("../../lib/common-utils");
var Config_Admin = require("../../config.admin.js");

module.exports = {
    //region Main
    Main: function (req, res, callback) {
        res.render("user-manage/main", callback);
    },
    //endregion
    //region Base_Manage
    Base_Manage: {
        User_List: function (req, res, callback) {
            res.render("user-manage/base-manage/user-list", callback);
        },
        User_Info: function (req, res, callback) {
            var _PageData = {
                flag: 0,
                User_ID: KOC.StringUtils.ToIntPositive(req.params.id)
            };
            if (!_PageData.User_ID) {
                _PageData.flag = "没有找到相关用户信息";
                CommonUtils.Common.PageScript_PageData(res, _PageData);
                res.render("user-manage/base-manage/user-info", callback);
                return;
            }
            KOC.Async.series([
                //读取用户信息
                function (callback) {
                    DALFactory.User.User_Info.Info(_PageData.User_ID, function (retValue) {
                        if (retValue.hasError) {
                            callback(retValue);
                            return;
                        }
                        if (!retValue.returnObject) {
                            retValue.hasError = true;
                            retValue.message = "没有相到相关用户信息";
                            callback(retValue);
                            return;
                        }
                        _PageData.User_Info = KOC.Lodash.pick(retValue.returnObject, [
                            "User_ID",
                            "NickName",
                            "RealName",
                            "IDCard",
                            "IDCard_Status",
                            "Signature",
                            "Email",
                            "Phone",
                            "QQ",
                            "Sex",
                            "Birthday",
                            "Constellation",
                            "Province",
                            "City",
                            "Channel_ID",
                            "Mounts_ID",
                            "Inviter_ID",
                            "SignUp_Anchor_ID",
                            "SignUp_Anchor_Family_ID",
                            "SignUp_Url",
                            "EXP",
                            "EXP_Next",
                            "EXP_Difference",
                            "Level",
                            "Level_Name",
                            "Level_Next",
                            "Level_Next_Name",
                            "VIP",
                            "VIP_Expire",
                            "VIP_ExpireDay",
                            "Coin",
                            "Coin_Today",
                            "Coin_Week",
                            "Coin_Month",
                            "Coin_Year",
                            "Coin_Total",
                            "Beans",
                            "Beans_Today",
                            "Beans_Week",
                            "Beans_Month",
                            "Beans_Year",
                            "Beans_Total",
                            "Pomelo",
                            "Pomelo_Get_Num",
                            "Pomelo_Get_Date",
                            "Fans",
                            "Attention",
                            "IsAnchor",
                            "IsFamily",
                            "IsAdmin",
                            "OAuth_QQ_ID",
                            "OAuth_Weibo_ID",
                            "OAuth_Weixin_ID",
                            "Charge_First",
                            "Charge_First_Date",
                            "Charge_First_Award",
                            "Charge_Last_Date",
                            "Login_Count",
                            "Login_This_Date",
                            "Login_This_IP",
                            "Create_Date",
                            "Create_IP",
                            "Status"
                        ]);
                        if (StateData.RightCode.Admin(req, res, "{User}[Base](Info-IDCard)") !== 0) {
                            _PageData.User_Info.RealName = KOC.StringUtils.Cover(_PageData.User_Info.RealName, "Name");
                            _PageData.User_Info.IDCard = KOC.StringUtils.Cover(_PageData.User_Info.IDCard, "IDCard");
                        }
                        if (StateData.RightCode.Admin(req, res, "{User}[Base](Info-Contact)") !== 0) {
                            _PageData.User_Info.Email = KOC.StringUtils.Cover(_PageData.User_Info.Email, "Email");
                            _PageData.User_Info.Phone = KOC.StringUtils.Cover(_PageData.User_Info.Phone, "Phone");
                            _PageData.User_Info.QQ = KOC.StringUtils.Cover(_PageData.User_Info.QQ, "QQ");
                        }
                        callback();
                    });
                },
                //读取用户公会信息
                function (callback) {
                    if (!_PageData.User_Info.IsFamily) {
                        callback();
                        return;
                    }
                    DALFactory.User.User_Family.Info(_PageData.User_ID, function (retValue) {
                        if (retValue.hasError) {
                            callback(retValue);
                            return;
                        }
                        if (!retValue.returnObject) {
                            retValue.hasError = true;
                            retValue.message = "没有找到用户公会信息";
                            callback(retValue);
                            return;
                        }
                        _PageData.User_Family = KOC.Lodash.pick(retValue.returnObject, [
                            "User_ID",
                            "Family_ID",
                            "IsMaster",
                            "RightCode",
                            "Create_Date",
                            "Create_IP",
                            "Status"
                        ]);
                        callback();
                    });
                },
                //读取用户座骑
                function (callback) {
                    DALFactory.User.User_Mounts.List(_PageData.User_ID, function (retValue) {
                        if (retValue.hasError) {
                            callback(retValue);
                            return;
                        }
                        _PageData.User_Mounts = retValue.returnObject;
                        callback();
                    });
                }
            ], function (retValue) {
                if (retValue) {
                    _PageData.flag = retValue.message + retValue.errorCode;
                }
                CommonUtils.Common.PageScript_PageData(res, _PageData);
                res.render("user-manage/base-manage/user-info", callback);
            });
        },
        User_Edit: function (req, res, callback) {
            var _PageData = {
                flag: 0,
                User_ID: KOC.StringUtils.ToIntPositive(req.params.id)
            };
            if (!_PageData.User_ID) {
                _PageData.flag = "没有找到相关用户信息";
                CommonUtils.Common.PageScript_PageData(res, _PageData);
                res.render("user-manage/base-manage/user-edit", callback);
                return;
            }
            DALFactory.User.User_Info.Info(_PageData.User_ID, function (retValue) {
                if (retValue.hasError) {
                    _PageData.flag = retValue.message + retValue.errorCode;
                } else if (!retValue.returnObject) {
                    _PageData.flag = "无此用户";
                } else {
                    _PageData.User_Info = KOC.Lodash.pick(retValue.returnObject, [
                        "User_ID",
                        "NickName",
                        "RealName",
                        "IDCard",
                        "IDCard_Status",
                        "Signature",
                        "Email",
                        "Phone",
                        "QQ",
                        "Sex",
                        "Birthday",
                        "Constellation",
                        "Province",
                        "City",
                        "Channel_ID",
                        "Mounts_ID",
                        "Inviter_ID",
                        "SignUp_Anchor_ID",
                        "SignUp_Anchor_Family_ID",
                        "SignUp_Url",
                        "EXP",
                        "EXP_Next",
                        "EXP_Difference",
                        "Level",
                        "Level_Name",
                        "Level_Next",
                        "Level_Next_Name",
                        "VIP",
                        "VIP_Expire",
                        "VIP_ExpireDay",
                        "Coin",
                        "Coin_Today",
                        "Coin_Week",
                        "Coin_Month",
                        "Coin_Year",
                        "Coin_Total",
                        "Beans",
                        "Beans_Today",
                        "Beans_Week",
                        "Beans_Month",
                        "Beans_Year",
                        "Beans_Total",
                        "Pomelo",
                        "Pomelo_Get_Num",
                        "Pomelo_Get_Date",
                        "Fans",
                        "Attention",
                        "IsAnchor",
                        "IsFamily",
                        "IsAdmin",
                        "OAuth_QQ_ID",
                        "OAuth_Weibo_ID",
                        "OAuth_Weixin_ID",
                        "Charge_First",
                        "Charge_First_Date",
                        "Charge_First_Award",
                        "Charge_Last_Date",
                        "Login_Count",
                        "Login_This_Date",
                        "Login_This_IP",
                        "Create_Date",
                        "Create_IP",
                        "Status"
                    ]);
                }
                CommonUtils.Common.PageScript_PageData(res, _PageData);
                res.render("user-manage/base-manage/user-edit", callback);
            });
        },
        User_IDCard_List: function (req, res, callback) {
            res.render("user-manage/base-manage/user-idcard-list", callback);
        },
        BankTrade_List: function (req, res, callback) {
            res.render("user-manage/base-manage/banktrade-list", callback);
        },
        PackageTrade_List: function (req, res, callback) {
            res.render("user-manage/base-manage/packagetrade-list", callback);
        }
    },
    //endregion
    //region Anchor_Manage
    Anchor_Manage: {
        List: function (req, res, callback) {
            res.render("user-manage/anchor-manage/list", callback);
        },
        Info: function (req, res, callback) {
            var _PageData = {
                flag: 0,
                User_ID: KOC.StringUtils.ToIntPositive(req.params.id)
            };
            if (!_PageData.User_ID) {
                _PageData.flag = "没有找到相关用户信息";
                CommonUtils.Common.PageScript_PageData(res, _PageData);
                res.render("user-manage/anchor-manage/info", callback);
                return;
            }
            KOC.Async.series([
                //读取用户信息
                function (callback) {
                    DALFactory.User.User_Info.Info(_PageData.User_ID, function (retValue) {
                        if (retValue.hasError) {
                            callback(retValue);
                            return;
                        }
                        if (!retValue.returnObject) {
                            retValue.hasError = true;
                            retValue.message = "没有相到相关用户信息";
                            callback(retValue);
                            return;
                        }
                        _PageData.User_Info = KOC.Lodash.pick(retValue.returnObject, [
                            "User_ID",
                            "NickName",
                            "RealName",
                            "IDCard",
                            "IDCard_Status",
                            "Signature",
                            "Email",
                            "Phone",
                            "QQ",
                            "Sex",
                            "Birthday",
                            "Constellation",
                            "Province",
                            "City",
                            "Channel_ID",
                            "Mounts_ID",
                            "Inviter_ID",
                            "SignUp_Anchor_ID",
                            "SignUp_Anchor_Family_ID",
                            "SignUp_Url",
                            "EXP",
                            "EXP_Next",
                            "EXP_Difference",
                            "Level",
                            "Level_Name",
                            "Level_Next",
                            "Level_Next_Name",
                            "VIP",
                            "VIP_Expire",
                            "VIP_ExpireDay",
                            "Coin",
                            "Coin_Today",
                            "Coin_Week",
                            "Coin_Month",
                            "Coin_Year",
                            "Coin_Total",
                            "Beans",
                            "Beans_Today",
                            "Beans_Week",
                            "Beans_Month",
                            "Beans_Year",
                            "Beans_Total",
                            "Pomelo",
                            "Pomelo_Get_Num",
                            "Pomelo_Get_Date",
                            "Fans",
                            "Attention",
                            "IsAnchor",
                            "IsFamily",
                            "IsAdmin",
                            "OAuth_QQ_ID",
                            "OAuth_Weibo_ID",
                            "OAuth_Weixin_ID",
                            "Charge_First",
                            "Charge_First_Date",
                            "Charge_First_Award",
                            "Charge_Last_Date",
                            "Login_Count",
                            "Login_This_Date",
                            "Login_This_IP",
                            "Create_Date",
                            "Create_IP",
                            "Status"
                        ]);
                        if (StateData.RightCode.Admin(req, res, "{User}[Base](Info-IDCard)") !== 0) {
                            _PageData.User_Info.RealName = KOC.StringUtils.Cover(_PageData.User_Info.RealName, "Name");
                            _PageData.User_Info.IDCard = KOC.StringUtils.Cover(_PageData.User_Info.IDCard, "IDCard");
                        }
                        if (StateData.RightCode.Admin(req, res, "{User}[Base](Info-Contact)") !== 0) {
                            _PageData.User_Info.Email = KOC.StringUtils.Cover(_PageData.User_Info.Email, "Email");
                            _PageData.User_Info.Phone = KOC.StringUtils.Cover(_PageData.User_Info.Phone, "Phone");
                            _PageData.User_Info.QQ = KOC.StringUtils.Cover(_PageData.User_Info.QQ, "QQ");
                        }
                        callback();
                    });
                },
                //读取艺人信息
                function (callback) {
                    DALFactory.User.User_Anchor.Info(_PageData.User_ID, function (retValue) {
                        if (retValue.hasError) {
                            callback(retValue);
                            return;
                        }
                        if (!retValue.returnObject) {
                            retValue.hasError = true;
                            retValue.message = "没有相到相关用户信息";
                            callback(retValue);
                            return;
                        }
                        _PageData.User_Anchor = retValue.returnObject;
                        if (StateData.RightCode.Admin(req, res, "{User}[Anchor](Info-Contact)") !== 0) {
                            _PageData.User_Anchor.Email = KOC.StringUtils.Cover(_PageData.User_Anchor.Email, "Email");
                            _PageData.User_Anchor.Phone = KOC.StringUtils.Cover(_PageData.User_Anchor.Phone, "Phone");
                            _PageData.User_Anchor.QQ = KOC.StringUtils.Cover(_PageData.User_Anchor.QQ, "QQ");
                        }
                        callback();

                    });
                }
            ], function (retValue) {
                if (retValue) {
                    _PageData.flag = retValue.message + retValue.errorCode;
                }
                CommonUtils.Common.PageScript_PageData(res, _PageData);
                res.render("user-manage/anchor-manage/info", callback);
            });
        },
        Edit: function (req, res, callback) {
            res.render("user-manage/anchor-manage/edit", callback);
        }
    },
    //endregion
    //region Family_Manage
    Family_Manage: {
        List: function (req, res, callback) {
            res.render("user-manage/family-manage/list", callback);
        },
        Info: function (req, res, callback) {
            var _PageData = {
                flag: 0,
                Family_ID: KOC.StringUtils.ToIntPositive(req.params.id)
            };
            if (!_PageData.Family_ID) {
                _PageData.flag = "没有找到相关用户信息";
                CommonUtils.Common.PageScript_PageData(res, _PageData);
                res.render("user-manage/family-manage/info", callback);
                return;
            }
            KOC.Async.series([
                //读取公会信息
                function (callback) {
                    DALFactory.User.Family_Info.Info(_PageData.Family_ID, function (retValue) {
                        if (retValue.hasError) {
                            callback(retValue);
                            return;
                        }
                        if (!retValue.returnObject) {
                            retValue.hasError = true;
                            retValue.message = "没有相到相关公会信息";
                            callback(retValue);
                            return;
                        }
                        _PageData.Family_Info = retValue.returnObject;
                        if (StateData.RightCode.Admin(req, res, "{User}[Family](Info-IDCard)") !== 0) {
                            _PageData.Family_Info.ContactName = KOC.StringUtils.Cover(_PageData.Family_Info.ContactName, "Name");
                            _PageData.Family_Info.IDCard = KOC.StringUtils.Cover(_PageData.Family_Info.IDCard, "IDCard");
                        }
                        if (StateData.RightCode.Admin(req, res, "{User}[Family](Info-Contact)") !== 0) {
                            _PageData.Family_Info.Email = KOC.StringUtils.Cover(_PageData.Family_Info.Email, "Email");
                            _PageData.Family_Info.Phone = KOC.StringUtils.Cover(_PageData.Family_Info.Phone, "Phone");
                            _PageData.Family_Info.QQ = KOC.StringUtils.Cover(_PageData.Family_Info.QQ, "QQ");
                        }
                        callback();

                    });
                },
                //读取用户信息
                function (callback) {
                    DALFactory.User.User_Info.Info(_PageData.Family_Info.User_ID, function (retValue) {
                        if (retValue.hasError) {
                            callback(retValue);
                            return;
                        }
                        if (!retValue.returnObject) {
                            retValue.hasError = true;
                            retValue.message = "没有相到相关用户信息";
                            callback(retValue);
                            return;
                        }
                        _PageData.User_Info = KOC.Lodash.pick(retValue.returnObject, [
                            "User_ID",
                            "NickName",
                            "RealName",
                            "IDCard",
                            "IDCard_Status",
                            "Signature",
                            "Email",
                            "Phone",
                            "QQ",
                            "Sex",
                            "Birthday",
                            "Constellation",
                            "Province",
                            "City",
                            "Channel_ID",
                            "Mounts_ID",
                            "Inviter_ID",
                            "SignUp_Anchor_ID",
                            "SignUp_Anchor_Family_ID",
                            "SignUp_Url",
                            "EXP",
                            "EXP_Next",
                            "EXP_Difference",
                            "Level",
                            "Level_Name",
                            "Level_Next",
                            "Level_Next_Name",
                            "VIP",
                            "VIP_Expire",
                            "VIP_ExpireDay",
                            "Coin",
                            "Coin_Today",
                            "Coin_Week",
                            "Coin_Month",
                            "Coin_Year",
                            "Coin_Total",
                            "Beans",
                            "Beans_Today",
                            "Beans_Week",
                            "Beans_Month",
                            "Beans_Year",
                            "Beans_Total",
                            "Pomelo",
                            "Pomelo_Get_Num",
                            "Pomelo_Get_Date",
                            "Fans",
                            "Attention",
                            "IsAnchor",
                            "IsFamily",
                            "IsAdmin",
                            "OAuth_QQ_ID",
                            "OAuth_Weibo_ID",
                            "OAuth_Weixin_ID",
                            "Charge_First",
                            "Charge_First_Date",
                            "Charge_First_Award",
                            "Charge_Last_Date",
                            "Login_Count",
                            "Login_This_Date",
                            "Login_This_IP",
                            "Create_Date",
                            "Create_IP",
                            "Status"
                        ]);
                        if (StateData.RightCode.Admin(req, res, "{User}[Base](Info-IDCard)") !== 0) {
                            _PageData.User_Info.RealName = KOC.StringUtils.Cover(_PageData.User_Info.RealName, "Name");
                            _PageData.User_Info.IDCard = KOC.StringUtils.Cover(_PageData.User_Info.IDCard, "IDCard");
                        }
                        if (StateData.RightCode.Admin(req, res, "{User}[Base](Info-Contact)") !== 0) {
                            _PageData.User_Info.Email = KOC.StringUtils.Cover(_PageData.User_Info.Email, "Email");
                            _PageData.User_Info.Phone = KOC.StringUtils.Cover(_PageData.User_Info.Phone, "Phone");
                            _PageData.User_Info.QQ = KOC.StringUtils.Cover(_PageData.User_Info.QQ, "QQ");
                        }
                        callback();
                    });
                }
            ], function (retValue) {
                if (retValue) {
                    _PageData.flag = retValue.message + retValue.errorCode;
                }
                CommonUtils.Common.PageScript_PageData(res, _PageData);
                res.render("user-manage/family-manage/info", callback);
            });

        },
        Edit: function (req, res, callback) {
            res.render("user-manage/family-manage/edit", callback);
        },
        BankTrade_List: function (req, res, callback) {
            res.render("user-manage/family-manage/banktrade-list", callback);
        }
    },
    //endregion
    //region Admin_Manage
    Admin_Manage: {
        List: function (req, res, callback) {
            res.render("user-manage/admin-manage/list", callback);
        },
        Info: function (req, res, callback) {
            var _PageData = {
                flag: 0,
                User_ID: KOC.StringUtils.ToInt(req.params.id, -1)
            };

            if (!_PageData.User_ID) {
                _PageData.flag = "没有找到相关管理员信息";
                CommonUtils.Common.PageScript_PageData(res, _PageData);
                res.render("user-manage/admin-manage/info", callback);
                return;
            }
            //读取管理员信息
            DALFactory.User.User_Admin.Info(_PageData.User_ID, function (retValue) {
                if (retValue.hasError) {
                    _PageData.flag = retValue.message + retValue.errorCode;
                } else if (!retValue.returnObject) {
                    _PageData.flag = "无此管理员";
                } else {
                    _PageData.User_Info = retValue.returnObject;
                    if (StateData.RightCode.Admin(req, res, "{User}[Base](Info-IDCard)") !== 0) {
                        _PageData.User_Info.RealName = KOC.StringUtils.Cover(_PageData.User_Info.RealName, "Name");
                    }
                    if (StateData.RightCode.Admin(req, res, "{User}[Base](Info-Contact)") !== 0) {
                        _PageData.User_Info.Phone = KOC.StringUtils.Cover(_PageData.User_Info.Phone, "Phone");
                    }
                    _PageData.User_Info = retValue.returnObject;
                }
                CommonUtils.Common.PageScript_PageData(res, _PageData);
                res.render("user-manage/admin-manage/info", callback);
            });
        },
        Edit: function (req, res, callback) {
            var _PageData = {
                flag: 0,
                User_ID: KOC.StringUtils.ToInt(req.params.id),
                RightCode: {
                    TreeData: [],
                    Data: [],
                    Default: [],
                    All: []
                },
                Status: -1
            };
            //权限
            var _RightCode = "";
            KOC.Async.series([
                //读取管理员信息
                function (callback) {
                    if (_PageData.User_ID <= 0) {
                        callback();
                        return;
                    }
                    DALFactory.User.User_Admin.Info(_PageData.User_ID, function (retValue) {
                        if (retValue.hasError) {
                            callback(retValue);
                            return;
                        }
                        if (!retValue.returnObject) {
                            retValue.hasError = true;
                            retValue.message = "无此管理员信息";
                            callback(retValue);
                            return;
                        }
                        _PageData.User_Info = retValue.returnObject;
                        _RightCode = _PageData.User_Info.RightCode;
                        callback(null);
                    });
                },
                //权限
                function (callback) {
                    KOC.Lodash.forEach(Config_Admin.MenuConfig, function (ThisValue_I) {
                        if (ThisValue_I.Setup === false) {
                            return true;
                        }
                        var _Item_II = [];
                        KOC.Lodash.forEach(ThisValue_I.Child, function (ThisValue_II) {
                            if (ThisValue_II.Setup === false) {
                                return true;
                            }
                            var _Item_III = [];
                            KOC.Lodash.forEach(ThisValue_II.Child, function (ThisValue_III) {
                                if (ThisValue_III.Setup === false) {
                                    return true;
                                }
                                _Item_III.push({
                                    Text: ThisValue_III.Text,
                                    Code: ThisValue_III.SetupCode || ThisValue_III.RightCode,
                                    Explain: ThisValue_III.Explain
                                });
                                if (_RightCode.indexOf(ThisValue_III.RightCode) >= 0) {
                                    _PageData.RightCode.Data.push(ThisValue_III.SetupCode || ThisValue_III.RightCode);
                                }
                                _PageData.RightCode.All.push(ThisValue_III.SetupCode || ThisValue_III.RightCode);
                            });
                            _Item_II.push({
                                Text: ThisValue_II.Text,
                                Code: ThisValue_II.RightCode,
                                Explain: ThisValue_II.Explain,
                                Checked: _RightCode.indexOf(ThisValue_II.RightCode) >= 0,
                                List: _Item_III
                            });
                        });
                        _PageData.RightCode.TreeData.push({
                            Text: ThisValue_I.Text,
                            Code: ThisValue_I.RightCode,
                            Explain: ThisValue_I.Explain,
                            Checked: _RightCode.indexOf(ThisValue_I.RightCode) >= 0,
                            List: _Item_II
                        });
                    });
                    _PageData.RightCode.Default = _PageData.RightCode.Data;
                    callback(null);
                }
            ], function (retValue) {
                if (retValue) {
                    _PageData.flag = retValue.message;
                }
                CommonUtils.Common.PageScript_PageData(res, _PageData);
                res.render("user-manage/admin-manage/edit", callback);
            });
        }
    }
    //endregion
};